<?php
/**
 * Created by PhpStorm.
 * User: HP_USER
 * Date: 2018/6/11
 * Time: 14:07
 */
namespace app\Clearing;

use app\common\Common;
use app\data\AdminData;
use app\data\UserData;
use think\Exception;
use think\Request;

class  Clearing extends Base
{

    private $status;

    private $info;

    public function __construct($status)
    {
        parent::__construct();
        $this->status = $status;
    }

    /**
     * 清算
     * @param $info
     * @return array
     * @throws Exception
     */
    public function clearing($info)
    {
        $this->info = $info;
        $res=$this->checkOrder();
        if($res['code']==$this->f_code) return $res;
        switch ($this->status) {
            case 'goods_order':
                return $this->goodsOrder($info);
            case 'proxy'://代理  省/市
                return $this->proxyClear($info);
            case 'withdraw':
                return $this->withdraw($info);
            default :
                throw new Exception('清算状态错误', $this->f_code);
        }
    }

    public function payClearing($info)
    {
        $this->info = $info;

        $res=$this->checkOrder();

        if($res['code']==$this->f_code) return $res;
        switch ($this->status) {
            case 'goods_order':
                return $this->payGoodsOrder($info);
            default :
                throw new Exception('清算状态错误', $this->f_code);
        }
    }

    private function checkOrder()
    {
        switch ($this->status) {
            case 'goods_order':

                return $this->checkGoodsOrder($this->info['order_num']);
            case 'proxy'://代理  省/市
                return $this->checkProxyClear($this->info['id']);
            case 'withdraw':
                return $this->withdraw($this->info);
            default :
                throw new Exception('清算状态错误', $this->f_code);
        }
    }

    /**
     * @param $order_num
     * @return array|int|mixed
     */
    private function checkGoodsOrder($order_num)
    {
        if (Common::checkTableFields('drh_user_flow', 'order_num', $order_num) == $this->t_code) {
            return ['code'=>$this->f_code,'msg'=>'非法操作'];
        }
        if (Common::checkTableFields('drh_shop_flow', 'order_num', $order_num) == $this->t_code) {
            return ['code'=>$this->f_code,'msg'=>'非法操作'];
        }
        return ['code'=>$this->t_code];
    }

    private function checkProxyClear()
    {

    }


    /**
     * 商品订单清算
     * @param $info
     * @return array
     */
    private function goodsOrder($info)
    {
        $user = new UserData();
        $this->info = $info;

        $distribution = new Distribution();

        $res = $distribution->userDistribution(
            $this->info['time'],
            $this->info['order_info'],
            $this->info['money'],
            $user
        );//用户分销

        $distribution->shopDistribution(
            $this->info['order_info'],
            $this->info['pay_status'],
            $this->info['time'],
            $this->info['money'],
            $res
        );//商户分销
        return ['code'=>$this->t_code];
    }

    public function payGoodsOrder($info)
    {

        $user = new UserData();
        $this->info = $info;
        $distribution = new Distribution();

        $res = $distribution->payUserDistribution(
            $this->info['time'],
            $this->info['order_info'],
            $this->info['money'],
            $user
        );//用户分销

        $distribution->payShopDistribution(
            $this->info['order_info'],
            $this->info['pay_status'],
            $this->info['time'],
            $this->info['money'],
            $res
        );//商户分销
        return ['code'=>$this->t_code];
    }

    /**
     * 购买用户订单结算
     * @param UserData $user
     * @param $order_info
     * @param $order_num
     * @param $type_status
     * @param $time
     * @return $this
     */
    public function userEnd(UserData $user, $order_info, $order_num, $time, $type_status = 1)
    {
        $user_info = $user->getInfo($order_info['user_id'], ['user_integral', 'user_money'], 'id');
        if ($order_info['give_integral'] != 0) {
            $user->addIntegral($order_info['user_id'], $order_info['give_integral']);
        }
        $user->addUserFlow($order_info['user_id'], $user_info['user_integral'], $user_info['user_money'],
            $time, 0, $order_info['give_integral'], $order_num, $type_status, $user_info['user_money'],
            $user_info['user_integral'] + $order_info['give_integral']);
        return $this;
    }

    /**
     * 代理清算
     * @param $info
     * @return array
     */
    private function proxyClear($info)
    {
        $time = time();
        $get_money = floor($info['money'] * $info['proportion'] / 100);//提成获得金额
        $user = new UserData();
        $user->addProxyMoney($info['id'], $get_money);
        $this->addUserProxyFlow($info['id'], $get_money, $time, 0, $info['lv']);
        $this->addAdminProxyFlow($info['id'], 2, $get_money, $time);
        return ['code'=>$this->t_code];
    }

    private function addUserProxyFlow($id, $money, $time, $fees = 0, $type_status = 1)
    {
        $res = Common::tableValue('drh_proxy', ['user_id', 'area_id', 'total_money', 'money'], ['area_id'], [$id]);
        $user = new UserData();
        $msg = ['total_start' => $res['total_money'],
            'fees' => $fees, 'money_expenses' => $money, 'end_money' => $res['total_money'] + $money];
        $user->addProxyFlow(
            $res['user_id'],
            $res['money'],
            $type_status,
            $money,
            $fees,
            $res['money'] + $money,
            json($msg),
            $time
        );
    }

    public function withdraw($info)
    {

    }

    public function addAdminProxyFlow($order_num, $type_status, $money, $time)
    {
        $admin = new AdminData();
        $res = $admin->getPlatformInfo();
        if ($type_status == 3) {//提现手续费
            $admin->addAdminProxyFlow($res['total_fees_money'], $order_num, $type_status,
                $money, $res['total_fees_money'] + $money, $time);
        } else {//
            $msg = ['total_start' => $res['total_platform_money'],
                'money_expenses' => $money, 'total_end' => $res['total_platform_money'] + $money];
            if (strlen($order_num) == 6) {
                $order_num = 'P-' . $order_num . '-' . date('Y-m-d', $time);
            }
            $admin->addAdminProxyFlow($res['platform_money'], $order_num, $type_status,
                $money, $res['platform_money'] + $money, $time, json($msg));
        }
    }
}